home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / devel / tcl / tclx7_31.z / tclx7_31 / tcldev / tclX7.3a-p1 / tests / flock.test < prev    next >
Encoding:
Text File  |  1994-01-23  |  6.8 KB  |  257 lines

  1. #
  2. # filecmds.test
  3. #
  4. # Tests for the flock and funlock commands.
  5. #---------------------------------------------------------------------------
  6. # Copyright 1992-1993 Karl Lehenbauer and Mark Diekhans.
  7. #
  8. # Permission to use, copy, modify, and distribute this software and its
  9. # documentation for any purpose and without fee is hereby granted, provided
  10. # that the above copyright notice appear in all copies.  Karl Lehenbauer and
  11. # Mark Diekhans make no representations about the suitability of this
  12. # software for any purpose.  It is provided "as is" without express or
  13. # implied warranty.
  14. #------------------------------------------------------------------------------
  15. # $Id: flock.test,v 3.1 1994/01/23 16:58:20 markd Exp $
  16. #------------------------------------------------------------------------------
  17. #
  18.  
  19. if {[info procs test] != "test"} then {source testlib.tcl}
  20.  
  21. unlink -nocomplain {FLOCKR.TMP FLOCKRW.TMP FLOCKW.TMP}
  22.  
  23. catch {flock} msg
  24. if {$msg == "File locking is not available on this system"} return
  25.  
  26. #
  27. # Fork without exec will not work under Tk, skip this test
  28. #
  29. if ![lempty [info commands button]] {
  30.     puts stderr "*************************************************************"
  31.     puts stderr "Flock tests are constructed in a way that does not work"
  32.     puts stderr "under Tk.  Test skipped."
  33.     puts stderr "*************************************************************"
  34.     puts stderr ""
  35.     return
  36. }
  37.  
  38. #
  39. # Create and open a read file and a write file.
  40. #
  41. unlink -nocomplain {FLOCKR.TMP FLOCKW.TMP}
  42.  
  43. foreach X {R W RW} {
  44.     set fh [open FLOCK${X}.TMP w]
  45.     puts $fh [replicate X 100]
  46.     close $fh
  47. }
  48.  
  49. set readFH  [open FLOCKR.TMP r]
  50. set writeFH [open FLOCKW.TMP w]
  51. set rdwrFH  [open FLOCKRW.TMP r+]
  52.  
  53. #
  54. # Check flock argument checking
  55. #
  56.  
  57. set flockWrongArgs {wrong # args: flock ?-read|-write? ?-nowait? fileId ?start? ?length? ?origin?}
  58.  
  59. Test flock-1.1 {flock argument checking} {
  60.     flock
  61. } 1 $flockWrongArgs
  62.  
  63. Test flock-1.2 {flock argument checking} {
  64.     flock $readFH 0 0 0 0
  65. } 1 $flockWrongArgs
  66.  
  67. Test flock-1.3 {flock argument checking} {
  68.     flock -writx $readFH
  69. } 1 {invalid option "-writx" expected one of "-read", "-write", or "-nowait"}
  70.  
  71. Test flock-1.4 {flock argument checking} {
  72.     flock -nowait
  73. } 1 $flockWrongArgs
  74.  
  75. Test flock-1.5 {flock argument checking} {
  76.     flock foofile
  77. } 1 {bad file identifier "foofile"}
  78.  
  79. Test flock-1.6 {flock argument checking} {
  80.     flock $readFH x
  81. } 1 {expected integer but got "x"}
  82.  
  83. Test flock-1.7 {flock argument checking} {
  84.     flock $readFH 1 x
  85. } 1 {expected integer but got "x"}
  86.  
  87. Test flock-1.8 {flock argument checking} {
  88.     flock $readFH {} x
  89. } 1 {expected integer but got "x"}
  90.  
  91. Test flock-1.9 {flock argument checking} {
  92.     flock $readFH {} 1 bad
  93. } 1 {bad origin "bad": should be "start", "current", or "end"}
  94.  
  95. Test flock-1.10 {flock argument checking} {
  96.     flock -read -write $rdwrFH 
  97. } 1 {can not specify both "-read" and "-write"}
  98.  
  99.  
  100. #
  101. # Check funlock argument checking
  102. #
  103.  
  104. set funlockWrongArgs {wrong # args: funlock fileId ?start? ?length? ?origin?}
  105.  
  106. Test flock-2.1 {funlock argument checking} {
  107.     funlock
  108. } 1 $funlockWrongArgs
  109.  
  110. Test flock-2.2 {funlock argument checking} {
  111.     funlock $readFH 0 0 0 0
  112. } 1 $funlockWrongArgs
  113.  
  114. Test flock-2.3 {funlock argument checking} {
  115.     funlock -write $readFH
  116. } 1  {bad file identifier "-write"}
  117.  
  118. Test flock-2.4 {funlock argument checking} {
  119.     funlock foofile
  120. } 1 {bad file identifier "foofile"}
  121.  
  122. Test flock-2.5 {funlock argument checking} {
  123.     funlock $readFH x
  124. } 1 {expected integer but got "x"}
  125.  
  126. Test flock-2.6 {funlock argument checking} {
  127.     funlock $readFH 1 x
  128. } 1 {expected integer but got "x"}
  129.  
  130. Test flock-2.7 {funlock argument checking} {
  131.     funlock $readFH {} x
  132. } 1 {expected integer but got "x"}
  133.  
  134. Test flock-2.8 {funlock argument checking} {
  135.     funlock $readFH {} 1 bad
  136. } 1 {bad origin "bad": should be "start", "current", or "end"}
  137.  
  138. #
  139. # If problems with acquiring locks, bail out now, as some tests may hang.
  140. # Suns are especially bad at hanging. It appears that having some data in
  141. # a file and only locking part of it will cause this test to fail rather
  142. # than hang on a confused system.
  143. #
  144. alarm 10
  145. puts $writeFH "This is some silly text to help prevent hangs"
  146. flush $writeFH
  147. if {[catch {flock $writeFH 0 8} msg] != 0} {
  148.     alarm 0
  149.     puts stderr "*************************************************************"
  150.     puts stderr "Error acquiring file lock.  This is probably caused by an"
  151.     puts stderr "incorrectly configured system or bug in the system software."
  152.     puts stderr "If NFS is involved, make sure lockd is correctly configured"
  153.     puts stderr "The error message returned was:"
  154.     puts stderr "    $msg"
  155.     puts stderr "*************************************************************"
  156.     catch {close $writeFH}
  157.     catch {close $readFH}
  158.     catch {close $rdwrFH}
  159.     unlink -nocomplain {FLOCKR.TMP FLOCKRW.TMP FLOCKW.TMP}
  160.     return
  161. }
  162. alarm 0
  163. funlock $writeFH
  164.  
  165. #
  166. # Check locking read/write access checking.
  167. #
  168.  
  169. Test flock-3.1 {flock argument checking} {
  170.     flock $readFH
  171. } 1 {file not open for writing}
  172.  
  173. Test flock-3.2 {flock argument checking} {
  174.     flock -write $readFH 
  175. } 1 {file not open for writing}
  176.  
  177. Test flock-3.3 {flock argument checking} {
  178.     flock -read $writeFH 
  179. } 1 {file not open for reading}
  180.  
  181. #
  182. # Check locking of a file that is not locked
  183. #
  184.  
  185. Test flock-4.1 {flock/unlock of a file that is not locked} {
  186.    flock $writeFH
  187.    funlock $writeFH
  188. } 0 {}
  189.  
  190. Test flock-4.2 {flock/unlock of a file that is not locked} {
  191.    flock -write $writeFH
  192.    funlock $writeFH
  193. } 0 {}
  194.  
  195. Test flock-4.3 {flock/unlock of a file that is not locked} {
  196.    flock -write $rdwrFH
  197.    funlock $rdwrFH
  198. } 0 {}
  199.  
  200. Test flock-4.2 {flock/unlock of a file that is not locked} {
  201.    flock -read $readFH
  202.    funlock $readFH
  203. } 0 {}
  204.  
  205. #
  206. # Start a process to lock a file.  A pipe will be used to report when its
  207. # locked.
  208.  
  209. pipe fromChild toParent
  210. flush stdout
  211. flush stderr
  212. set lockerPid [fork]
  213. if {$lockerPid == 0} {
  214.    flock $writeFH
  215.    flock $rdwrFH 0 10
  216.    puts $toParent "*I am ready*"
  217.    flush $toParent
  218.    while 1 {sleep 20}
  219.    exit 0
  220. }
  221. if {([gets $fromChild line] < 0) || ([set line] != "*I am ready*")} {
  222.    error "Unexpected response from flock test child: $line"}
  223.  
  224. Test flock-5.1 {flock of file locked by child process} {
  225.    flock -nowait $writeFH
  226. } 0 0
  227.  
  228. Test flock-5.2 {flock of file locked by child process} {
  229.    flock -nowait $rdwrFH 0 5
  230. } 0 0
  231.  
  232. Test flock-5.3 {flock of file locked by child process} {
  233.    flock -nowait $rdwrFH 0 5 start
  234. } 0 0
  235.  
  236. set rdwrSize [fstat $rdwrFH size]
  237.  
  238. Test flock-5.4 {flock of file locked by child process} {
  239.    flock -nowait $rdwrFH -$rdwrSize 5 end
  240. } 0 0
  241.  
  242. Test flock-5.4 {flock of file locked by child process} {
  243.    set stat [flock -nowait $rdwrFH 10 12 start]
  244.    funlock $rdwrFH 10 12 start
  245.    set stat
  246. } 0 1
  247.  
  248. kill $lockerPid
  249. wait $lockerPid
  250.  
  251. catch {close $readFH}
  252. catch {close $writeFH}
  253. catch {close $rdwrFH}
  254. catch {close $fromChild}
  255. catch {close $toParent}
  256. unlink -nocomplain {FLOCKR.TMP FLOCKRW.TMP FLOCKW.TMP}
  257.